package io.jft.doll.ui.home.bind

import com.alibaba.android.arouter.launcher.ARouter
import com.alibaba.android.vlayout.LayoutHelper
import com.alibaba.android.vlayout.layout.GridLayoutHelper
import com.g.base.extend.dp
import com.g.base.extend.observeNullableExOnce
import com.g.base.extend.resultNotNull
import com.g.base.extend.toast
import com.g.base.room.repository.Status
import com.g.base.token.TokenManage
import com.g.base.ui.recyclerView.item.BaseItem
import com.g.base.ui.recyclerView.setupRecyclerView
import io.jft.doll.R
import io.jft.doll.databinding.ItemQuickButtonBinding
import io.jft.doll.room.repository.UserRepository
import io.jft.doll.router.RouterPage

class QuickButtonBind : BaseItem<ItemQuickButtonBinding>() {
    override val layoutId: Int = R.layout.item_quick_button
    override val recyclerAble: Boolean = false

    override fun onBind(binding: ItemQuickButtonBinding) {
        val context = binding.root.context
        val adapter = setupRecyclerView(binding.recyclerView)
        val dp = 8.dp()
        adapter.layoutHelpers = arrayListOf(GridLayoutHelper(3, 6, dp) as LayoutHelper)
        val areaList = arrayListOf(R.drawable.ic_quick_1, R.drawable.ic_quick_2, R.drawable.ic_quick_3, R.drawable.ic_quick_4, R.drawable.ic_quick_5, R.drawable.ic_quick_6)
        for ((index, item) in areaList.withIndex()) {
            adapter.renderItems.add(ButtonItemBind(item).apply {
                setOnClickListener {
                    when (index) {
                        0 -> ARouter.getInstance().build(RouterPage.RoomListActivity)
                                .withString("title", "火爆区")
                                .withString("groupId", "6")
                                .navigation(context)
                        1 -> ARouter.getInstance().build(RouterPage.RoomListActivity)
                                .withString("title", "吸吸乐")
                                .withString("groupId", "2")
                                .navigation(context)
                        2 -> ARouter.getInstance().build(RouterPage.RoomListActivity)
                                .withString("title", "土豪区")
                                .withString("groupId", "1")
                                .navigation(context)
                        3 -> UserRepository().getDuiBarAddress()
                                .resultNotNull()
                                .observeNullableExOnce {
                                    when (it!!.status) {
                                        Status.Content -> ARouter.getInstance().build(RouterPage.WebViewActivity).withString("url", it.data!!).navigation(context)
                                        Status.Error -> context.toast(it.error?.message ?: "未知错误~")
                                    }
                                }
                        4 -> context.toast("努力开发中,敬请期待!")
                        5 -> {
                            val token = TokenManage.getToken()
                            ARouter.getInstance().build(RouterPage.DollActivity)
                                    .withString("userName", token?.nickName)
                                    .withString("thumbnail", token?.headerImage)
                                    .navigation(context)
                        }
                    }
                }
            })
        }
        adapter.notifyDataSetChanged()
    }
}